package com.sxfq.loansupermarket.backsystem.modular.basicData.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchVipLevelInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.VipLevelInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IVipLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/vipLevel")
public class VipLevelController extends BaseController {
    @Autowired
    IVipLevelService service;

    @Autowired
    private SqlMapper sqlMapper;

    private String PREFIX = "/basicData/vipLevel/";

    /**
     * 跳转到预约客户管理页面
     */
    @RequestMapping("")
    public String vipLevel(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "vipLevel.html";
    }

    /**
     * 跳转到预约客户管理添加页面
     */
    @RequestMapping(value = "/vipLevel_add")
    public String vipLevelAdd() {
        return PREFIX + "vipLevel_add.html";
    }

    @RequestMapping(value = "/to_integralRule")
    public String to_integralRule(Model model) {
        String sql="select integral_rule as integralRule,area from integral_rule_info limit 1";
        VipLevelInfo vipLevelInfo = sqlMapper.selectOne(sql,VipLevelInfo.class);
        model.addAttribute("vipLevelInfo", vipLevelInfo);
        model.addAttribute("area", vipLevelInfo.getArea().substring(0,7));
        return PREFIX + "integral_rule.html";
    }

    /**
     * 跳转到预约客户管理详情页面
     */
    @RequestMapping(value = "/vipLevel_update/{id}")
    public String vipLevelUpdate(@PathVariable String id, Model model) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        VipLevelInfo vipLevelInfo = this.service.selectOneById(id);
        model.addAttribute("vipLevelInfo", vipLevelInfo);
        return PREFIX + "vipLevel_edit.html";
    }

    /**
     * 预约客户管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchVipLevelInfo info, HttpServletRequest request) {
        Page<VipLevelInfo> page = new PageFactory<VipLevelInfo>().defaultPage();
        try {
            List<VipLevelInfo> list = service.selectPageListByQueryGetMap(page, info);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 添加预约客户
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public int add(@Valid VipLevelInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.insert(info);
    }

    /**
     * 删除预约客户
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public Tip delete(@PathVariable String id) throws Exception {
        service.delete(id);
        return SUCCESS_TIP;
    }

    /**
     * 修改考题
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public int update(@Valid VipLevelInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.update(info);
    }

    @RequestMapping(value = "/updateIntegralRule")
    @ResponseBody
    public int updateIntegralRule(@Valid VipLevelInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql="update integral_rule_info set integral_rule='"+info.getIntegralRule()+"'";
        return sqlMapper.update(sql);
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public  List<Map<String,Object>> getList() {
        List<Map<String,Object>> list= service.list();
        return list;
    }
}